$('#:employeeId').change -> refreshDebit()

$('#:debit').change -> refreshBalance()

$('#:name').change -> refreshDebit()

refreshDebit = ->
	employee = $('#:employeeId').val()
	if employee is ''
		return
	
	name = $('#:name').val()
	if name is ''
	  return
		
	data =
		'name': name
		'employeeId': employee
		'ajax': 'get-quota'
	success = (json) ->
		$('#:debit').val(json.quota)
		refreshBalance()
		return
	$.getJSON ':quotaUrl', data, success
	return

refreshBalance = ->
	employee = $('#:employeeId').val()
	if employee is ''
		return
	
	data =
		'employeeId': employee
		'ajax': 'get-balance'
	success = (json) ->
		debit = parseInt($('#:debit').val())
		debit = 0 if isNaN(debit) or ($('#:name').val() != ':annualy')
		$('#:balance').val(json.balance - debit)
		return
	$.getJSON ':balanceUrl', data, success
	return
